<template>
  <div class="dialog">
    <el-dialog :title="'批次' + titleText" :visible.sync="showDialog" width="55%" top="0" :close-on-click-modal="false"
      @close="cancel">
      <el-form ref="ruleForm" label-position="left" :model="formData" :rules="rules" class="el-form">
        <el-form-item label="类型名称:" prop="breedTypeNo" class="w50p">
          <el-select filterable clearable v-model="formData.breedTypeNo" filterable clearable allow-create>
            <el-option v-for="item of breedTypeNoList" :key="item.breedTypeId + 'breedType'" :label="item.breedTypeName"
              :value="item.breedTypeNo" />
          </el-select>
        </el-form-item>
        <el-form-item label="关联品种:" prop="varietyId" class="w50p">
          <el-select filterable clearable v-model="formData.varietyId" filterable clearable allow-create>
            <el-option v-for="item of FishVarietyData" :key="item.fishVarietyId + 'variety'" :label="item.varietyName"
              :value="item.fishVarietyId" />
          </el-select>
        </el-form-item>
        <el-form-item label="阶段名称:" prop="stageName" class="w50p">
          <el-input v-model="formData.stageName" clearable />
        </el-form-item>
        <el-form-item label="阶段天数:" prop="days" class="w50p">
          <el-input type="number" @change="(day) => { formData.days = day < 0 ? '' : day }" v-model="formData.days"
            clearable />
        </el-form-item>
        <el-form-item label="阶段顺序:" prop="orderIndex" class="w50p">
          <el-input type="number" @change="(orderIndex) => { formData.orderIndex = orderIndex < 0 ? '' : orderIndex }"
            v-model="formData.orderIndex" clearable />
        </el-form-item>
        <el-form-item label="备注:" class="w50p">
          <el-input v-model="formData.remarks" clearable />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import stageMsgApi from "@/api/traceability/basicsInfo/stageMsg";
import FishVarietyApi from "@/api/basic-info-manage/fishVariety"
import FishPondApi from "@/api/basic-info-manage/fishPond"
import BatchMangeApi from "@/api/batch-mange"
import BreedingTypeManageApi from "@/api/basic-info-manage/breeding-type-manage/index";
import ConfigCodeApi from "@/api/basic-info-manage/config-code";
import homeBaseApi from "@/api/basic-info-manage/homeBase";
import MaterialTypeApi from "@/api/basic-info-manage/material";
import MaterialTypeManageApi from "@/api/basic-info-manage/material-type-manage"

export default {
  name: '',
  components: {},
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: ""
    },
    nowChanceStau: {
      type: String,
      default: ""
    },
    show: {
      type: Boolean,
      default: false
    },
    editObject: {
      type: [Object],
      default: () => {
        return {}
      }
    }
  },
  data () {
    return {
      rules: {
        breedTypeNo: [
          { required: true, message: "请选择养殖类型", trigger: ["change", "blur"] }
        ],
        varietyId: [
          { required: true, message: "请选择养殖品种", trigger: ["change", "blur"] }
        ],
        stageName: [
          { required: true, message: "请输入阶段名称", trigger: ["change", "blur"] }
        ],
        days: [
          { required: true, message: "请输入阶段天数", trigger: ["change", "blur"] }
        ],
        orderIndex: [
          { required: true, message: "请输入阶段天数", trigger: ["change", "blur"] }
        ],
      },
      formData: {},
      breedTypeNoList: [],
      FishVarietyData: [],//品种
      showDialog: this.show,
    };
  },
  computed: {
  },
  watch: {
    show (v) {
      this.showDialog = v;
      this.$refs['ruleForm'] && this.$refs['ruleForm'].resetFields()
    },

    nowChanceStau (v) {
      if (v === 'add') {
        this.formData = {}
      }
      if (v === 'edit') {
        this.formData = this.editObject
      }
    }
  },
  created () { },
  mounted () {
    this.queryEnum()
  },
  methods: {
    // 查询枚举
    queryEnum () {
      BreedingTypeManageApi.queryByCondition().then(res => {
        this.breedTypeNoList = res.data || [];
      })
      FishVarietyApi.queryByCondition().then(res => {
        this.FishVarietyData = res.data
      });
    },
    save (formData) {
      this.$refs[formData].validate(valid => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            stageMsgApi.edit(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("修改成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            });
          } else if (this.nowChanceStau === "add") {
            stageMsgApi.add(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("添加成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    close () {
      this.$emit("closeDialog");
    },
    cancel () {
      this.$emit("cancelDialog");
    }
  }
};
</script>

<style lang="scss" scoped></style>
